# -*- coding:utf-8 -*-
# @Time : 2022/6/7 下午2:44 
# @Author : zdy
# @File : UISettings.py
# desc:

import os

from UI.Settings import Ui_Settings
from Class.Devices import *
from Class.DeviceBase import Userparam
from importQT import *
from UIClass.UIPowerParam import UIPowerParam


class UISettings(QDialog, Ui_Settings):
    def __init__(self):
        super(UISettings, self).__init__()
        self.setupUi(self)
        self.setWindowFlags(Qt.WindowCloseButtonHint)
        self.setFixedSize(self.width(), self.height())
        self.setAttribute(Qt.WA_DeleteOnClose)
        self.applyResource()
        self.spinBoxCurrent.setMaximum(10000)
        self.params = [Userparam.Current,
                       Userparam.Volt,
                       Userparam.WaterT,
                       Userparam.Reformer,
                       Userparam.Purification,
                       Userparam.GasPressure,
                       Userparam.Folw,
                       Userparam.Initcurrent,
                       Userparam.Initvolt,
                       Userparam.Waterspeed,
                       Userparam.Waterpressure, ]
        self.updateData()
        self.btnSave.clicked.connect(self.btnSave_clicked)
        self.btnPower.clicked.connect(self.btnPower_clicked)

    def applyResource(self):
        self.setWindowIcon(getIcon())

    def updateData(self):
        self.twParams.setColumnCount(4)
        # 设置表头粗体
        self.twParams.setHorizontalHeaderLabels(['类别', 'k2', 'k', 'c'])
        font = self.twParams.horizontalHeader().font()
        font.setBold(True)
        self.twParams.horizontalHeader().setFont(font)
        self.twParams.horizontalHeader().setSectionsClickable(False)  # 表头不可点击
        self.twParams.horizontalHeader().setStretchLastSection(True)  # 最后一列填充
        self.twParams.setItemDelegateForColumn(0, EditDelegate(self))
        self.twParams.setItemDelegateForColumn(1, EditDelegate1(self))
        self.twParams.setItemDelegateForColumn(2, EditDelegate1(self))
        self.twParams.setItemDelegateForColumn(3, EditDelegate1(self))
        headers = ["电流", "电压", "水温", "转化炉温", "净化炉温", "气压", '流量', '初始电流', '初始电压', '水流', '水压']
        for i, t in enumerate(self.params):
            row = self.twParams.rowCount()
            self.twParams.insertRow(row)
            param = device.findInUserparams(t)
            item = QTableWidgetItem()
            item.setData(Qt.DisplayRole, headers[i])
            self.twParams.setItem(row, 0, item)
            item1 = QTableWidgetItem()
            item1.setData(Qt.DisplayRole, param[0])
            self.twParams.setItem(row, 1, item1)
            item2 = QTableWidgetItem()
            item2.setData(Qt.DisplayRole, param[1])
            self.twParams.setItem(row, 2, item2)
            item3 = QTableWidgetItem()
            item3.setData(Qt.DisplayRole, param[2])
            self.twParams.setItem(row, 3, item3)

        self.twChannels.setColumnCount(4)
        # 设置表头粗体
        self.twChannels.setHorizontalHeaderLabels(['通道', '基线', '调零', '信号取反'])
        font = self.twChannels.horizontalHeader().font()
        font.setBold(True)
        self.twChannels.horizontalHeader().setFont(font)
        self.twChannels.horizontalHeader().setSectionsClickable(False)  # 表头不可点击
        self.twChannels.horizontalHeader().setStretchLastSection(True)  # 最后一列填充
        self.twChannels.setItemDelegateForColumn(0, EditDelegate(self))
        headers = ["LO", "HO", "N", "H", "H(热导)"]
        self.twChannels.insertRow(0)
        self.twChannels.insertRow(1)
        self.twChannels.insertRow(2)
        self.twChannels.insertRow(3)
        self.twChannels.insertRow(4)

        self.twChannels.setItem(0, 0, QTableWidgetItem(headers[0]))
        item01 = QTableWidgetItem()
        item01.setData(Qt.DisplayRole, device.systemconfig.daq.ADLE1)
        self.twChannels.setItem(0, 1, item01)
        item02 = QTableWidgetItem()
        item02.setData(Qt.DisplayRole, device.systemconfig.daq.DALE1)
        self.twChannels.setItem(0, 2, item02)
        item03 = QTableWidgetItem()
        item03.setCheckState(Qt.Checked if device.systemconfig.daq.ADLE1Inverse else Qt.Unchecked)
        self.twChannels.setItem(0, 3, item03)

        self.twChannels.setItem(1, 0, QTableWidgetItem(headers[1]))
        item11 = QTableWidgetItem()
        item11.setData(Qt.DisplayRole, device.systemconfig.daq.ADHE1)
        self.twChannels.setItem(1, 1, item11)
        item12 = QTableWidgetItem()
        item12.setData(Qt.DisplayRole, device.systemconfig.daq.DAHE1)
        self.twChannels.setItem(1, 2, item12)
        item13 = QTableWidgetItem()
        item13.setCheckState(Qt.Checked if device.systemconfig.daq.ADHE1Inverse else Qt.Unchecked)
        self.twChannels.setItem(1, 3, item13)

        self.twChannels.setItem(2, 0, QTableWidgetItem(headers[2]))
        item21 = QTableWidgetItem()
        item21.setData(Qt.DisplayRole, device.systemconfig.daq.ADLE2)
        self.twChannels.setItem(2, 1, item21)
        item22 = QTableWidgetItem()
        item22.setData(Qt.DisplayRole, device.systemconfig.daq.DALE2)
        self.twChannels.setItem(2, 2, item22)
        item23 = QTableWidgetItem()
        item23.setCheckState(Qt.Checked if device.systemconfig.daq.ADLE2Inverse else Qt.Unchecked)
        self.twChannels.setItem(2, 3, item23)

        self.twChannels.setItem(3, 0, QTableWidgetItem(headers[3]))
        item31 = QTableWidgetItem()
        item31.setData(Qt.DisplayRole, device.systemconfig.daq.ADLE3)
        self.twChannels.setItem(3, 1, item31)
        item32 = QTableWidgetItem()
        item32.setData(Qt.DisplayRole, device.systemconfig.daq.DALE3)
        self.twChannels.setItem(3, 2, item32)
        item33 = QTableWidgetItem()
        item33.setCheckState(Qt.Checked if device.systemconfig.daq.ADLE3Inverse else Qt.Unchecked)
        self.twChannels.setItem(3, 3, item33)

        self.twChannels.setItem(4, 0, QTableWidgetItem(headers[4]))
        item41 = QTableWidgetItem()
        item41.setData(Qt.DisplayRole, device.systemconfig.daq.ADLE3TC)
        self.twChannels.setItem(4, 1, item41)
        item42 = QTableWidgetItem()
        item42.setData(Qt.DisplayRole, device.systemconfig.daq.DALE3TC)
        self.twChannels.setItem(4, 2, item42)
        item43 = QTableWidgetItem()
        item43.setCheckState(Qt.Checked if device.systemconfig.daq.ADLE3TCInverse else Qt.Unchecked)
        self.twChannels.setItem(4, 3, item43)

        self.dspinboxVolt.setValue(device.systemconfig.alarmthreshold.volt)
        self.spinBoxCurrent.setValue(device.systemconfig.alarmthreshold.current)
        self.dSpinBoxPressure.setValue(device.systemconfig.alarmthreshold.carrierpressure)
        self.dSpinBoxFlow.setValue(device.systemconfig.alarmthreshold.carrierflow)
        self.dSpinBoxReformerDown.setValue(device.systemconfig.alarmthreshold.reformerdown)
        self.dSpinBoxReformerUp.setValue(device.systemconfig.alarmthreshold.reformerup)
        self.dSpinBoxPureDown.setValue(device.systemconfig.alarmthreshold.purificationdown)
        self.dSpinBoxPureUp.setValue(device.systemconfig.alarmthreshold.purficationup)
        self.dSpinBoxWaterF.setValue(device.systemconfig.alarmthreshold.waterF)
        self.dSpinBoxWaterT.setValue(device.systemconfig.alarmthreshold.waterT)

    def btnSave_clicked(self):
        for i, t in enumerate(self.params):
            data = [self.twParams.item(i, 1).data(0), self.twParams.item(i, 2).data(0),
                    self.twParams.item(i, 3).data(0), ]
            device.updateUserparams(t, data)

        device.systemconfig.daq.ADLE1 = self.twChannels.item(0, 1).data(0)
        device.systemconfig.daq.DALE1 = self.twChannels.item(0, 2).data(0)
        device.systemconfig.daq.ADLE1Inverse = self.twChannels.item(0, 3).checkState() == Qt.Checked

        device.systemconfig.daq.ADHE1 = self.twChannels.item(1, 1).data(0)
        device.systemconfig.daq.DAHE1 = self.twChannels.item(1, 2).data(0)
        device.systemconfig.daq.ADHE1Inverse = self.twChannels.item(1, 3).checkState() == Qt.Checked

        device.systemconfig.daq.ADLE2 = self.twChannels.item(2, 1).data(0)
        device.systemconfig.daq.DALE2 = self.twChannels.item(2, 2).data(0)
        device.systemconfig.daq.ADLE2Inverse = self.twChannels.item(2, 3).checkState() == Qt.Checked

        device.systemconfig.daq.ADLE3 = self.twChannels.item(3, 1).data(0)
        device.systemconfig.daq.DALE3 = self.twChannels.item(3, 2).data(0)
        device.systemconfig.daq.ADLE3Inverse = self.twChannels.item(3, 3).checkState() == Qt.Checked

        device.systemconfig.daq.ADLE3TC = self.twChannels.item(4, 1).data(0)
        device.systemconfig.daq.DALE3TC = self.twChannels.item(4, 2).data(0)
        device.systemconfig.daq.ADLE3TCInverse = self.twChannels.item(4, 3).checkState() == Qt.Checked

        device.systemconfig.alarmthreshold.volt = self.dspinboxVolt.value()
        device.systemconfig.alarmthreshold.current = self.spinBoxCurrent.value()
        device.systemconfig.alarmthreshold.carrierpressure = self.dSpinBoxPressure.value()
        device.systemconfig.alarmthreshold.carrierflow = self.dSpinBoxFlow.value()
        device.systemconfig.alarmthreshold.reformerdown = self.dSpinBoxReformerDown.value()
        device.systemconfig.alarmthreshold.reformerup = self.dSpinBoxReformerUp.value()
        device.systemconfig.alarmthreshold.purificationdown = self.dSpinBoxPureDown.value()
        device.systemconfig.alarmthreshold.purficationup = self.dSpinBoxPureUp.value()
        device.systemconfig.alarmthreshold.waterF = self.dSpinBoxWaterF.value()
        device.systemconfig.alarmthreshold.waterT = self.dSpinBoxWaterT.value()

        self.close()

    def btnPower_clicked(self):
        w = UIPowerParam()
        if w.exec() == 1:
            param = device.findInUserparams(Userparam.Current)
            self.twParams.item(0,1).setData(0,param[0])
            self.twParams.item(0, 2).setData(0, param[1])
            self.twParams.item(0, 3).setData(0, param[2])
            param = device.findInUserparams(Userparam.Volt)
            self.twParams.item(1, 1).setData(0, param[0])
            self.twParams.item(1, 2).setData(0, param[1])
            self.twParams.item(1, 3).setData(0, param[2])


class EditDelegate(QItemDelegate):
    def __init__(self, parent):
        super(EditDelegate, self).__init__(parent)

    def createEditor(self, QWidget, QStyleOptionViewItem, QModelIndex):
        return None


class EditDelegate1(QItemDelegate):
    def __init__(self, parent):
        super(EditDelegate1, self).__init__(parent)

    def createEditor(self, parent, option, index):
        editor = QDoubleSpinBox(parent)
        editor.setDecimals(3)
        editor.setRange(-1000, 1000)
        editor.setSingleStep(0.001)
        return editor
